// Fk framwork
var Fk = (function() {
    var _tagTable = ['section', 'div', 'p', 'a', 'header', 'footer', 'span'];
    //prototypes
    Element.prototype.hasClassName = function(name) {
        return new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)").test(this.className);
    };

    Element.prototype.addClassName = function(name) {
        if (!this.hasClassName(name)) {
            this.className = this.className ? [this.className, name].join(' ') : name;
        }
    };

    Element.prototype.removeClassName = function(name) {
        if (this.hasClassName(name)) {
            var c = this.className;
            this.className = c.replace(new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)", "g"), "");
        }
    };

    Element.prototype.bindEvent = function(event, callback) {
        _bindEvent(this, event, callback);
    };
    //public functions
    function _$(elt, context) {
        elt = elt.split('.');
        context = typeof context !== 'undefined' ? context : document;
        var tag = _tagTable.indexOf(elt[0]) !== -1 ? elt[0] : '*';
        if (elt[0] !== '' && tag === '*') {
            return document.getElementById(elt[0]);
        }
        else {
            var elts = context.getElementsByTagName(tag);
            if (typeof elt[1] !== 'undefined') {
                var patrn = new RegExp("(^|\\s)" + elt[1] + "(\\s|$)");
                var tagElts = elts;
                elts = new Array();
                [].forEach.call(tagElts, function(tagElt) {
                    if (patrn.test(tagElt.className))
                        elts.push(tagElt);
                });
            }
            if (elts.length === 1)
                return elts[0];
            else
                return elts;
        }
        return null;
    }

    function _trim(str) {
        return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    }

    function _bindEvent(el, event, callback) {
        if (window.addEventListener) {
            el.addEventListener(event, callback, false);
        } else {
            el.attachEvent(event, callback);
        }
    }

    function _bindWindowEvent(event, callback) {
        var funcWithRemoveListener = function() {
            var _event = event;
            var _callback = callback;

            _callback();
            if (window.removeEventListener) {
                window.removeEventListener(_event, funcWithRemoveListener, false);
            } else {
                window.detachEvent(_event, funcWithRemoveListener);
            }
        };

        _bindEvent(window, event, funcWithRemoveListener);
    }

    function _getComputedStyle(el, styleProp)
    {
        if (el.currentStyle)
            return el.currentStyle[styleProp];
        else if (window.getComputedStyle)
            return document.defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);

        return null;
    }

    function _whichTransitionEvent() {
        var t;
        var el = document.createElement('fakeelement');
        var transitions = {
            'transition': 'transitionEnd',
            'OTransition': 'oTransitionEnd',
            'MSTransition': 'msTransitionEnd',
            'MozTransition': 'transitionend',
            'WebkitTransition': 'webkitTransitionEnd'
        };

        for (t in transitions) {
            if (el.style[t] !== undefined) {
                return transitions[t];
            }
        }
        return null;
    }
    return {
        $: _$,
        bind: _bindWindowEvent,
        bindEvent: _bindEvent,
        trim: _trim,
        whichTransitionEvent: _whichTransitionEvent,
        getComputedStyle: _getComputedStyle
    };
}());

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20924025-2']);
_gaq.push(['_setDomainName', 'gairal.com']);
_gaq.push(['_trackPageview']);
Fk.global = (function() {
    function _fbLike() {
        var u = 'http://unkri.ch';
        var fbUrl = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u);
        window.open(fbUrl, 'sharer', 'toolbar=0,status=0,width=626,height=436');
        return false;
    }

    function _gPlusOne() {
        var po = document.createElement('script');
        po.type = 'text/javascript';
        po.async = true;
        po.src = 'https://apis.google.com/js/plusone.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(po, s);
    }

    function _gAnalytics() {
        var ga = document.createElement('script');
        ga.type = 'text/javascript';
        ga.async = true;
        ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(ga, s);
    }

    return {
        fbLike: _fbLike,
        gPlusOne: _gPlusOne,
        gAnalytics: _gAnalytics
    };
}());